package com.be.beadmin.basicData.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.basicData.entity.Workstation;
import com.be.beadmin.basicData.dto.output.WorkstationEx;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.basicData.service.WorkstationService;
import com.be.beadmin.basicData.dto.input.WorkstationQueryCriteria;
import com.be.beadmin.basicData.dto.input.WorkstationDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
* @author chenwei
* @date 2023-02-25
**/
@RestController
public class WorkstationController {

    @Autowired
    private  WorkstationService workstationService;


    @PostMapping(value = "/basicData/getWorkstationByPage")
    public ResultModel getWorkstationByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(workstationService.getWorkstationByPage(pageEntity)) ;
    }

    @PostMapping("/basicData/addWorkstation")
    public ResultModel addWorkstation(@RequestBody WorkstationDto workstationDto){
        workstationService.addWorkstation(workstationDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/basicData/delWorkstation")
    public ResultModel delWorkstation(@RequestBody WorkstationDto workstationDto){
        workstationService.delWorkstation(workstationDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/basicData/updateWorkstation")
    public ResultModel updateWorkstation(@RequestBody WorkstationDto workstationDto){
        workstationService.updateWorkstation(workstationDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/basicData/exportWorkstation")
    public void exportWorkstation(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WorkstationEx> workstationPage = workstationService.getWorkstationByPage(pageEntity);
        List<WorkstationEx> workstationExes = workstationPage.getContent();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("Workstation接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), WorkstationEx.class).sheet("sheet1").doWrite(workstationExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), WorkstationEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(workstationExes);
    }
}